package com.delio.server.produccion.articulos;

import java.sql.SQLException;
import java.util.List;

import com.delio.client.produccion.articulos.Articulo;
import com.delio.client.produccion.articulos.ArticuloService;
import com.delio.server.base.Conexion;
import com.delio.server.base.util.GxtComboBoxPaginate;
import com.delio.server.base.util.GxtGridPaginate;
import com.delio.server.series.Series;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.FilterPagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ibatis.sqlmap.client.SqlMapClient;

public class ArticuloServiceImpl extends RemoteServiceServlet implements ArticuloService {
private static final long serialVersionUID = 1L;
	
	static SqlMapClient sqlMap = Conexion.getSqlMap();

	
/*	@SuppressWarnings("unchecked")
	@Override
	public PagingLoadResult<Articulo> paginateArticulo(
			FilterPagingLoadConfig config,Boolean t) {
	    GxtGridPaginate paginate = new GxtGridPaginate();
		paginate.config = config;
		paginate.setLimit();
		if(paginate.order.equals("")){
			paginate.order = " order by id desc";
		}
		paginate.condition += " and tipo = "+t;
		List<Articulo> data = null;
		try {
			data = sqlMap.queryForList("paginateArticulo", paginate);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return new BasePagingLoadResult<Articulo>(data, paginate.offset, count(paginate));
	}*/
	
	@SuppressWarnings("unchecked")
	@Override
	public PagingLoadResult<Articulo> paginateArticulo(
			FilterPagingLoadConfig config) {
	    GxtGridPaginate paginate = new GxtGridPaginate();
		paginate.config = config;
		paginate.setLimit();
		if(paginate.order.equals("")){
			paginate.order = " order by id desc";
		}
		List<Articulo> data = null;
		try {
			data = sqlMap.queryForList("paginateArticulo", paginate);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return new BasePagingLoadResult<Articulo>(data, paginate.offset, count(paginate));
	}
	
	public Integer count(GxtGridPaginate paginate){
		Integer count = null;
		try {
			count = (Integer) sqlMap.queryForObject("countArticulo", paginate);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	
	
	
	@Override
	public boolean guardar(Articulo a) {
	   boolean var = false;
	   try{
		   a.setCodarticulo(getNumeracion());
		   sqlMap.insert("insertArticulo",a);
		   var = true;
	   }catch(Exception e){
		   e.printStackTrace();
	   }
		return var;
	}
	
	public boolean actualizar(Articulo a){
		boolean var = false;
		
		try{
		   a.setCodarticulo(getNumeracion(a.getCodarticulo()));
		   sqlMap.update("updateArticulo",a);	
		   var = true;	
		}catch(Exception e){
			e.printStackTrace();
		}
		return var;
	}
	
	private  static String getNumeracion(){
		Series.sqlMap = sqlMap;
		return Series.actualizarNumeracion("numeroarticulo");
	}
	
	private static String getNumeracion(String codigoedit){
		Series.sqlMap = sqlMap;
		Series.ultimonumero = codigoedit;		
		return Series.actualizarNumeracion("numeroarticulo");
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Articulo> ListarArticulos() {
		List<Articulo> la = null;
		try{
			la = sqlMap.queryForList("ListArticulos");
		}catch(Exception e){
			e.printStackTrace();
		}
		return la;
	}

	public boolean eliminar(Integer id){
		boolean var = false;
		try{
			sqlMap.delete("deleteArticulo",id);
			var = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return var;
	}
	
	@Override
	public PagingLoadResult<Articulo> paginateArticulo(
			BasePagingLoadConfig config) {
	 System.out.println("entro en paginloadresul BasePaginloadConfig");
		GxtComboBoxPaginate paginate = new GxtComboBoxPaginate();
		paginate.config = config;
		//Usuario user = getUsuarioSession();		
		//paginate.tienda_id = user.getIdtienda();
		
		return getPaginate(paginate);
	}
	
	 @SuppressWarnings("unchecked")
		public PagingLoadResult<Articulo> getPaginate(GxtComboBoxPaginate paginate){		  

		 	paginate.searchField="codigo";
			paginate.sortField="codigo";
			paginate.sortDir="ASC";
			
			paginate.setLimit();
			/*if(paginate.order.equals("")){
				paginate.order = " order by id desc";
			}*/
			List<Articulo> data = null;
			try {			
				data = sqlMap.queryForList("paginateComboBoxArticulo", paginate);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return new BasePagingLoadResult<Articulo>(data, paginate.offset, count(paginate));
		}
	
	public static Integer count(GxtComboBoxPaginate paginate){
	
		Integer count = null;
		try {
			count = (Integer) sqlMap.queryForObject("countComboBoxArticulo", paginate);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

}
